package io.renren.modules.device.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.device.entity.vo.DeviceVO;
import io.renren.modules.sys.entity.SysUserTokenEntity;
import io.renren.modules.sys.service.ShiroService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.device.entity.TDeviceEntity;
import io.renren.modules.device.service.TDeviceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 设备表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-03-29 11:51:23
 */
@RestController
@RequestMapping("generator/tdevice")
public class TDeviceController {
    @Autowired
    private TDeviceService tDeviceService;
    @Autowired
    private ShiroService shiroService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:tdevice:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tDeviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{code}")
    @RequiresPermissions("generator:tdevice:info")
    public R info(@PathVariable("code") String code) {
        DeviceVO tDevice = tDeviceService.getDeviceInfoById(code);

        return R.ok().put("tDevice", tDevice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:tdevice:save")
    public R save(@RequestBody TDeviceEntity tDevice, @RequestHeader(value = "token") String token) {

        try {
            SysUserTokenEntity userTokenEntity = shiroService.queryByToken(token);
            tDevice.setCreator(String.valueOf(userTokenEntity.getUserId()));
            tDeviceService.save(tDevice);

            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:tdevice:update")
    public R update(@RequestBody TDeviceEntity tDevice) {
        tDeviceService.updateById(tDevice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:tdevice:delete")
    public R delete(@RequestBody String[] codes) {
        tDeviceService.removeByIds(Arrays.asList(codes));

        return R.ok();
    }

}
